package cn.benma666.sjsj.ljq.fzgj;

import cn.benma666.dict.Zdlb;
import cn.benma666.domain.SysSjglFile;
import cn.benma666.exception.BusinessException;
import cn.benma666.exception.MyException;
import cn.benma666.iframe.DictManager;
import cn.benma666.iframe.MyParams;
import cn.benma666.iframe.Result;
import cn.benma666.myutils.HttpByOk;
import cn.benma666.myutils.HttpUtil;
import cn.benma666.myutils.WebUtil;
import cn.benma666.sjsj.web.DefaultLjq;
import cn.benma666.vo.WjscVo;
import com.alibaba.druid.util.Utils;
import com.alibaba.fastjson.JSONObject;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

/**
 * 穿透http请求
 */
public class CtHttpLjq extends DefaultLjq {

    public Result insert(MyParams myParams) throws MyException {
        JSONObject rep = new JSONObject();
        Result r = doHttp(myParams, null,rep);
        JSONObject data = new JSONObject();
        data.put(Result.DATA,r.getData());
        data.put("repHeards",rep);
        r.setData(data);
        return r;
    }

    public Result upload(MyParams myParams) throws Exception {
        SysSjglFile yobj = myParams.yobj(SysSjglFile.class);
        return doHttp(myParams, yobj,null);
    }
    @Override
    public Result download(MyParams myParams) {
        HttpUtil yobj = myParams.yobj(HttpUtil.class);
        JSONObject rep = new JSONObject();
        byte[] resByte;
        try {
            resByte = Utils.readByteArray(getInputStream(yobj, null, rep));
        } catch (IOException e) {
            throw new BusinessException("穿透http请求结果流读取失败",e);
        }
        String filename;
        String filetype = "";
        String fieldValue = rep.getString("Content-Disposition");
        if (fieldValue == null || !fieldValue.contains("filename=")) {
            String url = yobj.getUrl();
            String[] s = url.split("/");
            filename = s[s.length - 1];
        } else {
            filename = fieldValue.substring(fieldValue.indexOf("filename=") + 10, fieldValue.length() - 1);
        }
        String[] s1 = filename.split("\\.");
        filetype = s1[s1.length - 1];
        SysSjglFile file1 = new SysSjglFile();
        file1.setWjm(filename);
        file1.setWjlx(filetype);
        return super.resultFile(resByte, file1);
    }

    public Result doHttp(MyParams myParams, SysSjglFile file, JSONObject rep) {
        HttpUtil yobj = myParams.yobj(HttpUtil.class);
        try {
            String resStr = new String(Utils.readByteArray(getInputStream(yobj,file,rep)), yobj.getQqbm());
            return success("请求成功", resStr);
        } catch (IOException e) {
            throw new BusinessException("穿透http请求结果流读取失败",e);
        }

    }
    public InputStream getInputStream(HttpUtil yobj, SysSjglFile file,JSONObject rep) {
        HttpUtil http = new HttpUtil(HttpByOk.getInstance());

        if (isBlank(yobj.getUrl())) {
            throw new MyException("请求地址不能为空");
        }
        JSONObject rp = yobj.getRp();
        JSONObject params = yobj.getParam()==null?new JSONObject(): (JSONObject) yobj.getParam();
        // 设置参数格式
        if (isBlank(rp.getString(HttpUtil.CONTENT_TYPE))) {
            rp.put(HttpUtil.CONTENT_TYPE, WebUtil.CONTENTTYPE_APPLICATION_JSON);
        }
        // 设置上传文件
        if (!isBlank(file)) {
            rp.put(HttpUtil.CONTENT_TYPE,"multipart/form-data");
            String type = DictManager.zdMcByDm(Zdlb.SYS_COMMON_XZTLX.name(), file.getWjlx());
            if (file.getWjlx().equals(type)) {
                type = "application/octet-stream";
            }
            RequestBody requestBody = RequestBody.create(Base64.getDecoder().decode(file.getWjnr()), MediaType.parse(type));
            WjscVo wjscVo = new WjscVo(file.getWjm(), requestBody);
            params.put(yobj.getFileKey(), wjscVo);
        }
        // 发起http请求
        return http.getInputStream(yobj.getUrl(), params, yobj.getQqbm(), yobj.getCssc(), rp, rep);
    }

}
